

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;

//import oracle.jdeveloper.layout.XYLayout;
//import oracle.jdeveloper.layout.XYConstraints;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.SwingConstants;

public class InterfaceTriangle extends JFrame 
{

  private static final String ISOCELE = "Isocèle";
  private static final String EQUI = "Équilatéral";
  private static final String RECTANGLE = "Rectangle";
  private static final String SCALENE = "Scalène";
  private static final String INVALIDE = "Invalide";
  private static final String PHRASE = "C'est un triangle ";


  private JTextField coteA = new JTextField();
  private JTextField coteB = new JTextField();
  private JTextField coteC = new JTextField();
  private JLabel jLabel1 = new JLabel();  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private JButton btnValider = new JButton();
  private JButton btnEffacer = new JButton();
  private JButton btnQuitter = new JButton();
  private JLabel rep = new JLabel();
  private JPanel jPanel1 = new JPanel();
  private JLabel repTexte = new JLabel();
  private JLabel repImage = new JLabel();
  private String A, B, C;
  private Triangle triangle;
  private String typeTriangle;

  public InterfaceTriangle()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  private void jbInit() throws Exception
  {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(606, 376));
    
    coteA.setBounds(new Rectangle(80, 45, 75, 25));
    coteA.setNextFocusableComponent(coteB);
    coteB.setBounds(new Rectangle(80, 80, 75, 25));
    coteB.setNextFocusableComponent(coteC);
    coteC.setBounds(new Rectangle(80, 115, 75, 25));
    jLabel1.setText("Côté A :");
    jLabel1.setBounds(new Rectangle(30, 45, 50, 25));
    jLabel1.setFont(new Font("Dialog", 1, 12));
    jLabel1.setRequestFocusEnabled(false);
    jLabel2.setText("Côté B :");
    jLabel2.setBounds(new Rectangle(30, 80, 50, 25));
    jLabel2.setFont(new Font("Dialog", 1, 12));
    jLabel2.setRequestFocusEnabled(false);
    jLabel3.setText("Côté C :");
    jLabel3.setBounds(new Rectangle(30, 115, 50, 25));
    jLabel3.setFont(new Font("Dialog", 1, 12));
    jLabel3.setRequestFocusEnabled(false);
    btnValider.setText("Valider");
    btnValider.setBounds(new Rectangle(170, 45, 80, 25));
    btnValider.setFont(new Font("Dialog", 1, 12));
    btnValider.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
          btnValider_actionPerformed(e);
        }
    });
    btnEffacer.setText("Effacer");
    btnEffacer.setBounds(new Rectangle(170, 80, 80, 25));
    btnEffacer.setFont(new Font("Dialog", 1, 12));
    btnEffacer.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnEffacer_actionPerformed(e);
        }
      });
    btnQuitter.setText("Quitter");
    btnQuitter.setBounds(new Rectangle(170, 115, 80, 25));
    btnQuitter.setFont(new Font("Dialog", 1, 12));
    btnQuitter.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnQuitter_actionPerformed(e);
        }
      });
    rep.setText("Repr�sentation :");
    rep.setBounds(new Rectangle(260, 25, 140, 20));
    rep.setFont(new Font("Dialog", 1, 14));
    jPanel1.setBounds(new Rectangle(280, 55, 295, 250));
    jPanel1.setLayout(null);
    jPanel1.setMaximumSize(new Dimension(5, 5));
    jPanel1.setRequestFocusEnabled(false);
    repTexte.setBounds(new Rectangle(30, 185, 225, 35));
    repTexte.setBorder(BorderFactory.createLineBorder(new Color(138, 138, 138), 1));
    repTexte.setVerticalTextPosition(SwingConstants.TOP);
    repTexte.setVerticalAlignment(SwingConstants.TOP);
    repImage.setBounds(new Rectangle(5, 5, 290, 240));
    repImage.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    repImage.setMinimumSize(new Dimension(1, 1));
    repImage.setMaximumSize(new Dimension(5, 5));
    repImage.setRequestFocusEnabled(false);
    repImage.setPreferredSize(new Dimension(2, 2));
    this.getContentPane().add(repTexte, null);
    jPanel1.add(repImage, null);
    this.getContentPane().add(jPanel1, null);
    this.getContentPane().add(rep, null);
    this.getContentPane().add(btnQuitter, null);
    this.getContentPane().add(btnEffacer, null);
    this.getContentPane().add(btnValider, null);
    this.getContentPane().add(jLabel3, null);
    this.getContentPane().add(jLabel2, null);
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(coteC, null);
    this.getContentPane().add(coteB, null);
    this.getContentPane().add(coteA, null);
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent evt)
      {
        exitForm(evt);
      }
    });
  }
  public static void main(String args[]) {
        InterfaceTriangle tp1 = new InterfaceTriangle();
        tp1.setTitle("Suis-je un triangle ?");
        tp1.setSize(600, 400);
        tp1.setVisible(true);
        
    }

  private void btnEffacer_actionPerformed(ActionEvent e)
  {
  
    repTexte.setText("");  
    repImage.setText(""); 
    repImage.setIcon(null);
  }

  private void btnQuitter_actionPerformed(ActionEvent e)
  {
    ;
  }
  private void exitForm(WindowEvent e)
  {
    System.exit(0);
  }

  private void btnValider_actionPerformed(ActionEvent e)
  {
    
     A = coteA.getText();
     B = coteB.getText();
     C = coteC.getText();
     
     
    if(A.length() == 0 && B.length()== 0 && C.length() == 0)
      javax.swing.JOptionPane.showMessageDialog(null, "ATTENTION! Les valeurs nulles sont interdites.");       
    else
    {
      //v�rifier si un nombre
      try
      {
        
          int numA = verifNum(A);
          int numB = verifNum(B);
          int numC = verifNum(C);
          String img;
          try{
            triangle = new Triangle(numA, numB, numC);
            typeTriangle = triangle.getTypeTriangle();
            repTexte.setText(PHRASE+typeTriangle);
            img = typeTriangle+".gif";
            this.repImage.setIcon(new ImageIcon(this.getClass().getResource(img)));            
          

          }
          catch(Exception eTriangle)
          {
              repTexte.setText("Ce n'est pas un triangle");
              img = INVALIDE+".gif";
             this.repImage.setIcon(new ImageIcon(this.getClass().getResource(img)));        
              

          }
          
        }
        catch(NumberFormatException e1)
        {
        javax.swing.JOptionPane.showMessageDialog(null, "ATTENTION! Les c�t�s doivent �tre des entiers.");       
      }
    }

  }
  
private int verifNum(String s) 
{
  int numA;
  
    numA = Integer.parseInt(s);
    return numA;
  
}
}